package cn.tedu.gateway.filters;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

//@Component
public class LogGatewayFilterFactory
        extends AbstractGatewayFilterFactory<LogGatewayFilterFactory.Config> {

     public LogGatewayFilterFactory(){
         super(Config.class);
     }
    @Override
    public List<String> shortcutFieldOrder() {
         return Arrays.asList("consoleLog","cacheLog");
    }

    @Override
    public GatewayFilter apply(Config config) {

         return new GatewayFilter() {
             @Override
             public Mono<Void> filter(ServerWebExchange exchange
                     , GatewayFilterChain chain) {
                 if (config.isConsoleLog()){
                     System.out.println("consoleLog日志已开启");
                 }else {
                     System.out.println("consoleLog日志关闭");
                 }
                 if (config.isCacheLog()){
                     System.out.println("cacheLog日志已开启");
                 }else {
                     System.out.println("cacheLog日志关闭");
                 }
                 return chain.filter(exchange);
             }
         };
    }

    public static class Config{
        private boolean consoleLog;
        public boolean cacheLog;

        public boolean isConsoleLog() {
            return consoleLog;
        }

        public void setConsoleLog(boolean consoleLog) {
            this.consoleLog = consoleLog;
        }

        public boolean isCacheLog() {
            return cacheLog;
        }

        public void setCacheLog(boolean cacheLog) {
            this.cacheLog = cacheLog;
        }
    }
}
